package com.mxfcode.back.controller;

import com.alibaba.fastjson.JSONObject;
import com.mxfcode.annotation.LoginRequired;
import com.mxfcode.bbs_common.util.AOSUtils;
import com.mxfcode.bbs_common.util.StringUtils;
import com.mxfcode.common.MyBlogUtils;
import com.mxfcode.common.UploadFileUtils;
import com.mxfcode.constants.UploadConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * @类名 UploadController
 * @描述 上传图片控制器
 * @作者 mxf
 * @创建日期 2021-03-03 18:18
 **/
@Controller
@RequestMapping("/common")
@LoginRequired
public class UploadController {

    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> uploadFileByEditormd(HttpServletRequest request,MultipartFile file,
                                                                 HttpServletResponse response) throws URISyntaxException, IOException {
        String type = request.getParameter("type");
        Map<String,String> uploadConstantMap = getUploadType(type);
        if((AOSUtils.isEmpty(uploadConstantMap))){
            throw new RuntimeException("获取上传文件存储路径常量为空");
        }
        String suffixName = UploadFileUtils.getSuffixName(file);
        //生成文件名称通用方法
        String newFileName = UploadFileUtils.getNewFileName(suffixName);
        File fileDirectory = new File(uploadConstantMap.get("file_upload"));
        //创建文件
        File destFile = new File(uploadConstantMap.get("file_upload") + newFileName);
        Map<String,Object> result = new HashMap<>();
        String fileUrl = null;
        Map<String, Object> data = new HashMap<>();
        try {
            if (!fileDirectory.exists()) {
                if (!fileDirectory.mkdirs()) {
                    throw new IOException("文件夹创建失败,路径为：" + fileDirectory);
                }
            }
            file.transferTo(destFile);
            fileUrl = MyBlogUtils.getHost(new URI(request.getRequestURL() + "")) +
                    uploadConstantMap.get("sql_upload") + newFileName;
            data.put("url", fileUrl);
            data.put("code", 200);
            data.put("msg", "成功");
            data.put("size", file.getSize());
            return data;
        } catch (UnsupportedEncodingException e) {
            data.put("url", null);
            data.put("code", 200);
            data.put("msg", "失败");
            data.put("size", file.getSize());
        } catch (IOException e) {
            result.put("success", 0);
        }
        return data;
    }

    public static Map<String,String> getUploadType(String type){
        Map<String,String> resultMap = new HashMap<>();
        if("plate".equals(type)){
            resultMap.put("file_upload",UploadConstants.FILE_UPLOAD_PLATE);
            resultMap.put("sql_upload",UploadConstants.SQL_UPLOAD_PLATE);
        }
        if("article".equals(type)){
            resultMap.put("file_upload",UploadConstants.FILE_UPLOAD_ARTICLE);
            resultMap.put("sql_upload",UploadConstants.SQL_UPLOAD_ARTICLE);
        }
        if("user".equals(type)){
            resultMap.put("file_upload",UploadConstants.FILE_UPLOAD_USER);
            resultMap.put("sql_upload",UploadConstants.SQL_UPLOAD_USER);
        }
        return resultMap;
    }
}
